package com.example.lib_mvp.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;

public class GradientTextView extends AppCompatTextView {

    private int[] colors = {0xFF3A78FF, 0xFF813BFF}; //蓝紫渐变  渐变颜色
    private int[] colors_black = {0xFF666666, 0xFF666666}; // 灰黑色 渐变颜色

    private boolean isShape = false;
    float width = getWidth();
    Paint paint = getPaint();
    LinearGradient shader = new LinearGradient(0, 0, width, 0,
            colors, null, Shader.TileMode.CLAMP);
    LinearGradient shader_black = new LinearGradient(0, 0, width, 0,
            colors_black, null, Shader.TileMode.CLAMP);

    public void setShape(boolean shape) {
        isShape = shape;
        invalidate();
    }

    public GradientTextView(Context context) {
        super(context);
    }

    public GradientTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (isShape) {
            paint.setShader(shader);
        } else {
            paint.setShader(shader_black);
        }
        super.onDraw(canvas);
    }
}